package com.database.objects;

import java.util.*;

import com.google.common.collect.Lists;

public enum Status {

	ADD("ADDED"), MODIFY("MODIFIED"), DELETE("DELETED"), REPLACE("REPLACED"), OPEN("OPEN"), CLOSED("CLOSED"), INVALID("INVALID"), UNKNOWN("UNKNOWN"), TOTAL("TOTAL");

	private static Map<Status, List<String>> similarMap = new HashMap<Status, List<String>>();

	static {
		List<String> similarOpen = Lists.newArrayList("ACCEPTED", "NEW", "STARTED", "PENDING");
		List<String> similarClosed = Lists.newArrayList("DONE", "FIXED", "VERIFIED", "WONTFIX", "WORKSFORME");
		List<String> similarInvalid = Lists.newArrayList("DELETED", "DUPLICATE");

		similarMap.put(OPEN, similarOpen);
		similarMap.put(CLOSED, similarClosed);
		similarMap.put(INVALID, similarInvalid);
	}

	private String text;

	private Status(String text) {
		this.text = text;
	}

	public String getText() {
		return text;
	}

	public static Status createStatus(String value) {
		if (value.equals("A")) {
			return ADD;
		} else if (value.equals("M")) {
			return MODIFY;
		} else if (value.equals("D")) {
			return DELETE;
		} else if (value.equals("R")) {
			return REPLACE;
		} else {
			value = processValue(value);
			try {
				return valueOf(value);
			} catch (Exception e) {
				Status similarValue = obtainSimilarValue(value);
				if (similarValue.isUnknown()) {
					e.printStackTrace();
				}
				return similarValue;
			}
		}
	}

	public static String processValue(String value) {
		return value.trim().replace(" ", "").toUpperCase();
	}

	private static Status obtainSimilarValue(String value) {
		for (Status key : similarMap.keySet()) {
			for (String similar : similarMap.get(key)) {
				if (value.equals(similar)) {
					return key;
				}
			}
		}
		return UNKNOWN;
	}

	public boolean isAdd() {
		return equals(ADD);
	}

	public boolean isModify() {
		return equals(MODIFY);
	}

	public boolean isDelete() {
		return equals(DELETE);
	}

	public boolean isReplace() {
		return equals(REPLACE);
	}

	public boolean isOpen() {
		return equals(OPEN);
	}

	public boolean isClosed() {
		return equals(CLOSED);
	}

	public boolean isInvalid() {
		return equals(INVALID);
	}

	public boolean isUnknown() {
		return equals(UNKNOWN);
	}

	public boolean isTotal() {
		return equals(TOTAL);
	}

	public static Status valueOfText(String text) {
		for (Status status : values()) {
			if (status.getText().equals(text)) {
				return status;
			}
		}
		return null;
	}

}